#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2003 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
####################################################################
#
#   unnamedyet command
#
#   Syntax:  link_check { {-v | -l}  --help }
#   -v = use verify link
#   -l = use testlinecont 
#   --help = ask for help
####################################################################
#
#   Programmer: Wesley D. Jones
#   Initial version - 9/24/03 
#
#   11/14/03
#   Updated support for multiple networks/planes
#   Spelling fixes
####################################################################
use Getopt::Long;							#includes
use Fcntl;

$PRGRM  = "link_check";							#program name
$BIN    = "/opt/hsc/bin/command";					#location
$lsswmanprop = "/opt/hsc/bin/command/lsswmanprop";			#location paths for all needed aps
$lsswtopol = "/opt/hsc/bin/command/lsswtopol";
$verifylink = "/opt/hsc/bin/command/verifylink";
$testlinecont = "/opt/hsc/bin/command/testlinecont";

$flag=@ARGV[0];             					        #command line argument #1
$USAGE = "Usage: link_check  { {-v | -l}  --help }\n";			#usage statement 

if ($#ARGV == -1) {                             			#if no arguments selected print USAGE and exit
        print "No arguments selected.\n";
        print $USAGE;
        exit;
}

if ($flag eq "-v")                              			#if verify link selected set flag
{
        #print "\nwill parse via verifylink....\n";
        $parse_type=1;
} elsif ($flag eq "-l") {                       			#if testlinecount selected, set flag
        #print "\nwill parse via testlinecont....\n";
        $parse_type=2;		
} elsif ($flag eq "--help") {                   			#if help selected print USAGE, and exit
        #print "\n--HELP--\n";
        print $USAGE;
        exit;
} else {                                       				 #if invalid arg print USAGE, and exit
        print $USAGE;
        exit;
}

print "\nRetrieving the network and plane information...";
open(README, "$lsswmanprop -t top -F network:plane: |") or die "Can't run program: $!\n";   #open 1st app to collect network/plane data

while(<README>) {									    #collect ouput from app into string
        chomp $_;
	$output .= $_;
}
chomp $output;										    #Take newline out for dual plane system *
close(README);										    #close app


@chars = unpack("A1" x length($output), $output);					    #chop up string by char and store into array

$network = @chars[0];									    #array locations [0] and [2] become nework and plane		
$plane = @chars[2];
$break_loop = 0;
$end_ptr = 2;
if ($network eq '' or $plane eq '')							    #if either blank then quit
{
	print "\nError gathing information...Check that daemon is running\n";
	exit;
} 
$output = '';
print "Complete\n";
while ($break_loop != 1)
{
	#print "Network->$network Plane->$plane\n";
	print "Gathering switch frame and cage data for Network->$network Plane->$plane ...";
	open(README, "$lsswtopol -n $network -p $plane -F frame:cage: |") or die "Can't run program: $!";   #open app for finding frame/cage info
	#$output = '';											      #clear string for reuse
	while(<README>) {										      #read program output into string
        	$output .= $_;
	}
	close(README);
	print "Complete\n";
	$end_ptr += 2;
	$net_plane = @chars[$end_ptr];
	if ($net_plane eq '')
	{
	        $break_loop=1;	
	}
	else
	{
		$network = @chars[$end_ptr];
		#print "\nnew network $network";
		$plane = @chars[$end_ptr+2];
		#print "\nnew plane $plane";
		$end_ptr += 2;
		#print "Complete\n";
	}
}
#print "\n$output\n";											      #print output to screen(debug)

@array = unpack("A1" x length($output), $output);						      #chop up string by char and store into array
chomp $output;											      #chomp newline(\n) from string...before last statement?

$flag = 0;											      #init all variables for main loop (flag for switch focus c/f)
$count = 0;											      #ptr for location in current location in array
$start_tapeing = 0;									              #used for when to start storing info for ouput/log

open(LOGFILE, "> /var/hsc/log/link_check.log") or die "Can't write /var/hsc/log/link_check.log: $!";  #open logfile for writing and/or create
print LOGFILE "Begin Log Session...\n\n";
close(LOGFILE);											      #close logfile

while ($count <= length($output))							              #while not at end of array
{
        $temp = @array[$count];									      #take current element from array and store into temp
        if ($temp =~ ":")								              #if it was a colon...
        {
                if ($flag == 0)									      #and focus was frames now
                {
                        $flag = 1;						                      #switch focus to cages and next iteration = good cage data
                }
                elsif ($flag == 1)								      #now at right cage position
                 {
                        print "\nVerifying link status of --> Frame: $frame_holder Cage: $cage_holder";       #announce current frame/cage combo

                        if ($parse_type ==1){								#parse via verifylink
				print "\nPlease wait a few minutes...";
                                open(README, "$verifylink -f $frame_holder -g $cage_holder |") or die "Can't run program: $!\n";
                        }
                        elsif ($parse_type == 2) {
				print "\nPlease wait, this can take quite some time...";		#parse via testlinecont
                                 open(README, "$testlinecont -f $frame_holder -g $cage_holder -n |") or die "Can't run program: $!\n";
                        }
			#organize output
			$output_new .=  "-------------------------------------Frame: $frame_holder Cage: $cage_holder-----------------------------------------\n";
                        while(<README>) {					#read ouput 
				if ($_ =~ /unsuccessful/)			#if reached error portion start storing data 
				{
					$start_tapeing = 1;
				}
				if ($start_tapeing == 1)
				{
                                	$output_new .= $_;
				}
                        }
                        $output_new .=  "-------------------------------------Frame: $frame_holder Cage: $cage_holder-----------------------------------------\n";

			close(README);						#close app
			print $output_new;					#print data details to screen
			$total_output .= $output_new;				#store current data into overall data for screen clarity and log
			
			$output_new = '';					#re-initialize all data
			$start_tapeing = 0;
                        $flag = 0;
                        $frame_holder = '';
                        $cage_holder = '';
                }
        }
        elsif ($flag == 0)										#if no colon and flag ==0 then store frame
        {
                $frame_holder .= $temp;
        }
        elsif ($flag == 1)										#if no colon and flag == 1 then store cage
        {
                $cage_holder .= $temp;
        }
        $count++;											#move ptr one step
}
print "\nWriting information to /var/hsc/log/link_check.log\n"; 
open(LOGFILE, ">> /var/hsc/log/link_check.log")     or die "Can't write /var/hsc/log/link_check.log: $!";	#open log again and append log data 
print LOGFILE "$total_output";												
close(LOGFILE);													#close log file

